package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.PageResult;
import com.itheima.constant.QueryPageBean;
import com.itheima.constant.Result;
import com.itheima.domain.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/14 19:17
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    //分页查询;
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.findPage(queryPageBean);
        return pageResult;
    }

    //添加;
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    //删除数据;
    @RequestMapping("/deleteByid")
    public Result deleteByid(Integer id) {
        try {
            permissionService.deleteByid(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    //编辑页面用于数据回显;
    @RequestMapping("/findByid")
    public Result findByid(Integer id) {
        try {
            Permission permission = permissionService.findByid(id);
            return new Result(true, "回显成功", permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    //编辑数据;
    @RequestMapping("/update")
    public Result update(@RequestBody Permission permission) {
        try {
            permissionService.update(permission);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    //查询全部信息;
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> list = permissionService.findAll();
            return new Result(true,"查询成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
}
